package com.appsys.controller;

import com.appsys.pojo.*;
import com.appsys.service.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName ListController
 * @Description TODO
 * @Author YinLiangTao Email:1517535831@qq.com
 * @Date 2022/2/26 15:42
 * @Version 1.0
 */
@Controller
public class BackendController {
    @Resource
    private AppInfoService appInfoService;
    @Resource
    private StatusService statusService;
    @Resource
    private FlatformService flatformService;
    @Resource
    private FirstLevelService firstLevelService;
    @Resource
    private SecondLevelService secondLevelService;
    @Resource
    private ThirdLevelService thirdLevelService;
    @Resource
    private DevService devService;
    @Resource
    private VersionService versionService;

    @RequestMapping(value = "/backendlist.html")
    public String toList(){
        return "backend/backendlist";
    }

    @RequestMapping(value = "/backendlist/{pageNumber}",method={RequestMethod.GET,RequestMethod.POST})
    public String List(@PathVariable(value = "pageNumber") int pageNumber,Model model,
                       @RequestParam(value = "softwareName",required = false,defaultValue = "null") String softwareName,
                       @RequestParam(value = "flatformId",required = false,defaultValue = "0") Integer flatformId,
                       @RequestParam(value = "categoryLevel1",required = false,defaultValue = "0") Integer categoryLevel1,
                       @RequestParam(value = "categoryLevel2",required = false,defaultValue = "0") Integer categoryLevel2,
                       @RequestParam(value = "categoryLevel3",required = false,defaultValue = "0") Integer categoryLevel3,
                       @RequestParam(value = "status",required = false,defaultValue = "1") Integer status){


        HashMap<Object, Object> map = new HashMap<>();
        if( "null".equals(softwareName) || softwareName == null ||  "".equals(softwareName)){

        }else {
            map.put("softwareName",softwareName);
        }
        if(flatformId != 0){
            map.put("flatformId",flatformId);
        }

        map.put("status",status);

        if(categoryLevel1 != 0){
            map.put("categoryLevel1",categoryLevel1);
            List<SecondLevel> secondLevels = secondLevelService.getSecondLevels(categoryLevel1);
            model.addAttribute("secondLevels",secondLevels);
        }
        if(categoryLevel2 != 0){
            map.put("categoryLevel2",categoryLevel2);
            List<ThirdLevel> thirdLevels = thirdLevelService.getThirdLevels(categoryLevel2);
            model.addAttribute("thirdLevels",thirdLevels);
        }
        if(categoryLevel3 != 0){
            map.put("categoryLevel3",categoryLevel3);
        }
        model.addAttribute("formMap",map);
        //(页号，页大小)
        PageHelper.startPage(pageNumber,5);
        //startPage后面紧跟的就是一个分页查询
        List<AppInfo> appInfoByDevId = appInfoService.getAppInfoByDevId(map);
        //用PageInfo对结果进行包装(传入连续显示的页数)
        PageInfo pageInfo = new PageInfo(appInfoByDevId, 5);
        model.addAttribute("pageInfo",pageInfo);
        System.out.println(pageInfo);
        System.out.println(pageInfo.getList().size());
        System.out.println(pageInfo.getPageSize());
        //获取版本号对应的版本名
        List<Version> versions = new ArrayList<>();
        for (int i = 0;i < appInfoByDevId.size();i++) {
            Integer versionId = appInfoByDevId.get(i).getVersionId();
            System.out.println(versionId);
            if(versionId != null){
                versions.add(versionService.getVersionById(versionId));
            }else {
                Version version = new Version();
                versions.add(version);
            }
        }
        model.addAttribute("versions",versions);
        //获取全部的状态信息
        List<Status> status1 = statusService.getStatus();
        model.addAttribute("status",status1);
        //获取全部的平台信息
        List<Flatform> flatforms = flatformService.getFlatforms();
        model.addAttribute("flatforms",flatforms);
        //获取全部的一级分类
        List<FirstLevel> firstLevels = firstLevelService.getFirstLevels();
        model.addAttribute("firstLevels",firstLevels);
        System.out.println("----------------------");
        return "forward:/backendlist.html";
    }

    @RequestMapping(value = "/backendlistWithAjax/{pageNumber}",method = RequestMethod.GET)
    @ResponseBody
    public Msg getList(@PathVariable(value = "pageNumber") int pageNumber, Model model,
                       @RequestParam(value = "id",required = false) Integer id,
                       @RequestParam(value = "softwareName",required = false,defaultValue = "null") String softwareName,
                       @RequestParam(value = "flatformId",required = false,defaultValue = "0") Integer flatformId,
                       @RequestParam(value = "categoryLevel1",required = false,defaultValue = "0") Integer categoryLevel1,
                       @RequestParam(value = "categoryLevel2",required = false,defaultValue = "0") Integer categoryLevel2,
                       @RequestParam(value = "categoryLevel3",required = false,defaultValue = "0") Integer categoryLevel3,
                       @RequestParam(value = "status",required = false,defaultValue = "1") Integer status){

        HashMap<Object, Object> map = new HashMap<>();
        if(id != null){
            map.put("id",id);
        }
        if( "null".equals(softwareName) || softwareName == null ||  "".equals(softwareName)){

        }else {
            System.out.println(softwareName);
            map.put("softwareName",softwareName);
        }
        if(flatformId != 0){
            map.put("flatformId",flatformId);
        }

            map.put("status",status);

        if(categoryLevel1 != 0){
            map.put("categoryLevel1",categoryLevel1);
        }
        if(categoryLevel2 != 0){
            map.put("categoryLevel2",categoryLevel2);
        }
        if(categoryLevel3 != 0){
            map.put("categoryLevel3",categoryLevel3);
        }
        //(页号，页大小)
        PageHelper.startPage(pageNumber,5);
        //startPage后面紧跟的就是一个分页查询
        List<AppInfo> appInfoByDevId = appInfoService.getAppInfoByDevId(map);
        //用PageInfo对结果进行包装(传入连续显示的页数)
        PageInfo pageInfo = new PageInfo(appInfoByDevId, 5);
        model.addAttribute("pageInfo",pageInfo);
        //获取版本号对应的版本名
        List<Version> versions = new ArrayList<>();
        for (int i = 0;i < appInfoByDevId.size();i++) {
            Integer versionId = appInfoByDevId.get(i).getVersionId();
            System.out.println(versionId);
            if(versionId != null){
                versions.add(versionService.getVersionById(versionId));
            }else {
                Version version = new Version();
                versions.add(version);
            }
        }
        model.addAttribute("versions",versions);
        return Msg.success().add("pageInfo",pageInfo).add("versions",versions);
    }

    //跳转app所有的信息页面
    @RequestMapping(value = "/toBackendAppInfoAndVersion/{id}/{versionId}/{pageNumber}")
    public String toAppInfoAndVersion( @PathVariable(value = "id") int id,
                                      @PathVariable(value = "versionId") int versionId,
                                      @PathVariable(value = "pageNumber") int pageNumber, Model model) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("id", id);
        List<AppInfo> appInfoByDevId = appInfoService.getAppInfoByDevId(map);
        String logoPath = appInfoByDevId.get(0).getLogoPath();
        if (logoPath != null) {
            logoPath.replace("\\", "\\u005c\\u005c");
            File file = new File(logoPath);
            String logoPathName = file.getName();
            model.addAttribute("logoPathImg", "static/uploadfiles/" + logoPathName);
        }
        model.addAttribute("appInfo", appInfoByDevId);

        Version versionById = versionService.getVersionById(versionId);
        model.addAttribute("versions", versionById);
        model.addAttribute("id", id);
        model.addAttribute("pageNumber", pageNumber);
        return "backend/appInfoAndVersion";
    }

    //更新APP状态
    @RequestMapping(value = "/updateStatus/{appId}/{status}/{pageNumber}")
    public String  updateStatus(@PathVariable(value = "appId") int appId,
                                @PathVariable(value = "pageNumber") int pageNumber,
    @PathVariable(value = "status") int status){
        appInfoService.updateStatusByappId(appId,status);
        return "redirect:/backendlist/" + pageNumber;
    }
}
